package com.ming.interview;

import java.util.Arrays;
import java.util.Stack;

/**
 * @Description 题目:阿拉伯数字与中文数字互转
 * @Author ye.ming
 * @Date Created in 2019/8/26
 */
public class Main23 {

    public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);
//        System.out.println("请输入要转化的数字:");
//        int num = sc.nextInt();
//        long num = chineseToNum("二亿零一万二千三百零一");
//        System.out.println(num);
//        sc.close();

        char c = '1';
        int a = c;
        System.out.println(c - '0');
    }


    /**
     * 数字转中文
     * @param num
     * @return
     */
    public static String numToChinese(int num) {
        //String[] chapters = new String[] { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        //String[] chapters = new String[] { "", "拾", "佰", "仟" };
        String[] chapters = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] sections = new String[]{"", "万", "亿", "万亿"};
        String[] units = new String[]{"", "十", "百", "千"};
        if (num == 0) {
            return chapters[num];
        }

        //将数字转为一个int数组,倒序
        String value = String.valueOf(num);
        int length = value.length();
        int[] numArr = new int[length];
        for (int i = 0; i < length; i++) {
            numArr[length - 1 - i] = value.charAt(i) - '0';
        }


        Stack<String> stack = new Stack<>();
        for (int j = 0; j < length; j++) {
            int unit = j % 4;
            int sect = j / 4;
            if (unit == 0) {
                //举例100002000,也就是处理一节(四位数字)都是零的情况
                if (!stack.empty() && Arrays.asList(sections).contains(stack.peek())) {
                    stack.pop();
                    if (!stack.empty() && stack.peek() != "零") {
                        stack.push("零");
                    }
                }
                stack.push(sections[sect]);
            }
            if (numArr[j] != 0) {
                stack.push(units[unit]);
            }
            if (numArr[j] == 0) {
                //如果当前数字为0,且栈里面已经有"零"了,或者有"节"单位(万,亿)了
                if (stack.peek() == "零" || Arrays.asList(sections).contains(stack.peek())) {
                    continue;
                }
            }
            stack.push(chapters[numArr[j]]);
        }
        String result = "";
        //结果拼成一个字符串
        while (!stack.empty()) {
            result += stack.pop();
        }
        //去掉结尾为零的情况.
        if (result.endsWith("零")) {
            result = result.substring(0, result.lastIndexOf("零"));
        }
        return result;
    }

    public static long chineseToNum(String numStr) {
        //String[] chapters = new String[] { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        //String[] chapters = new String[] { "", "拾", "佰", "仟" };
        String[] chapters = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] sections = new String[]{"", "万", "亿"};
        String[] units = new String[]{"", "十", "百", "千"};
        char[] chars = numStr.toCharArray();
        Stack<String> stack = new Stack<>();
        for (int i = chars.length - 1; i >= 0; i--) {
            stack.push(String.valueOf(chars[i]));
        }
        long result = 0;
        //表示权值,即:一千,十万,五百,对应的数字放在temp里面
        long temp = 0;
        //表示节值,即:一千三百五十二,三十二万,五百八十六亿,对应的数字放在tempSection里面
        long tempSection = 0;
        while (!stack.empty()) {
            String s = stack.pop();
            //当前数字为零,则跳过
            if ("零".equals(s)) {
                continue;
            }
            //如果是数字,则放在temp里面
            if (Arrays.asList(chapters).contains(s)) {
                temp = Arrays.asList(chapters).indexOf(s);
                //如果此时stack已经空了,则直接求出最终结果;
                if (stack.empty()) {
                    tempSection += temp;
                    result += tempSection;
                    //否则,判断stack是否已经到了一节的结尾,如果是,则加入到tempSection里面
                } else if (Arrays.asList(sections).contains(stack.peek())) {
                    tempSection += temp;
                }
                //如果是数字的权值,则跟数字计算之后,放入到temp里面,并加到tempSection
            } else if (Arrays.asList(units).contains(s)){
                temp = (long) (temp * Math.pow(10, Arrays.asList(units).indexOf(s)));
                tempSection += temp;
                temp=0;
                //如果此时stack已经空了,则需要计算出最终结果
                if (stack.empty()) {
                    result += tempSection;
                }
                //如果是节值(万,亿),则计算tempSection,并加入到result里面.
            } else if (Arrays.asList(sections).contains(s)){
                tempSection = (long) (tempSection * Math.pow(10000, Arrays.asList(sections).indexOf(s)));
                result += tempSection;
                tempSection = 0;
            }
        }
        return result;
    }


}
